<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom, Plus
} from '@element-plus/icons-vue'
    import avatar from '@/assets/default.png'
    //条目被点击后,调用的函数
    import {useRouter} from 'vue-router'
    const router = useRouter();
    import {useTokenStore} from '@/store/token.js'
    const tokenStore = useTokenStore();
    import adminApi from "@/api/admin.js";
    import {useAdminInfoStore} from '@/store/adminInfo.js'
    import {ref} from "vue";
import {ElMessage} from "element-plus";
    const adminInfoStore = useAdminInfoStore()

    const getAdminInfo = () =>{
        adminApi.adminInfo().then(result =>{
            if (result.code == 0){
                //数据存储到pinia中
                adminInfoStore.setAdminInfo(result.data.admin)
                menuData.value = result.data.routers
                adminInfoStore.setBtns(result.data.btns)
                // 将管理员ID存储到tokenStore中
                if (result.data.admin && result.data.admin.id) {
                    tokenStore.setUserId(result.data.admin.id)
                    console.log('设置管理员ID到tokenStore:', result.data.admin.id)
                }
            }
        })
    }
    getAdminInfo()

// 菜单  用户管理， 分类管理， 商品管理
const menuData = ref([
    {
        name: '权限管理', icon: 'GobletFull', children: [
            {name: '管理员管理', icon: 'GobletSquareFull', path: "/admin"},
            {name: '角色管理', icon: 'TrendCharts', path: "/role"},
            {name: '权限管理', icon: 'TrendCharts', path: "/permission"},
        ]
    },
    {
        name: '商品分类管理', icon: 'Notebook', children: [
            {name: '商品管理', icon: 'Document', path: "/product"},
            {name: '分类管理', icon: 'Calendar', path: "/category"},
        ]
    },
    {
        name: '健身房管理', icon: 'House', children: [
            {name: '课程管理', icon: 'Document', path: "/course"},
            {name: '教练管理', icon: 'User', path: "/coach"},
            {name: '用户管理', icon: 'UserFilled', path: "/user"},
            {name: '预约管理', icon: 'Calendar', path: "/reserve"},
        ]
    }
])

const adminPasswordDTO = ref({
    oldPassword:'',
    newPassword:''
})
//重置密码
const dialogResetPasswordDialog = ref(false)
const resetForm = ref()

//自定义确认密码的校验函数
const rePasswordValid = (rule, value, callback) => {
    if (value == null || value === '') {
        return callback(new Error('请再次确认密码'))
    }
    //响应式对象要：registerData.value才能拿到值
    if (adminPasswordDTO.value.newPassword !== value) {
        return callback(new Error('两次输入密码不一致'))
    }

    callback()
}
const rules = ref({
    oldPassword: [
        {required: true, message: '请输入密码', trigger: 'blur'},
        {min: 3, max: 16, message: '密码长度必须为3~16位', trigger: 'blur'}
    ],
    newPassword: [
        {required: true, message: '请输入密码', trigger: 'blur'},
        {min: 3, max: 16, message: '密码长度必须为3~16位', trigger: 'blur'}
    ],
    reNewPassword: [
        {required: true, message: '请输入密码', trigger: 'blur'},
        {validator: rePasswordValid, trigger: 'blur' }
    ]
})
const resetPassword = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            adminApi.resetPassword(adminPasswordDTO.value).then(result => {
                if (result.code === 0) {
                    ElMessage.success(result.msg)
                    dialogResetPasswordDialog.value = false
                    tokenStore.removeToken();
                    adminInfoStore.removeAdminInfo();
                    // 跳转到登录
                    router.push('/login')
                } else {
                    ElMessage.error(result.msg)
                }
            })
        } else {
            ElMessage.error('表单验证失败');
        }
    })
}


    const dialogFormVisible = ref(false)
    const admin = ref({})
    const handleCommand = (command) => {
        //判断指令
        if (command === 'logout') {
            //退出登录
            tokenStore.removeToken()
            //跳出到登录界面
            router.push('/login')
        } else if (command === 'info'){
            dialogFormVisible.value=true
            // admin.value = adminInfoStore.admin
            Object.assign(admin.value,adminInfoStore.admin)
        }else if (command === 'resetPassword'){
            dialogResetPasswordDialog.value = true
            adminPasswordDTO.value = {}
            resetForm.value.resetFields()
        }
    }

//图片上传
const handleAvatarSuccess = (result) =>{
    admin.value.avatar = result.data
}

const headers = ref({
    //携带token传递到后端
    Authorization: tokenStore.token
});

const updateAdminInfo = () =>{
    adminApi.update(admin.value).then(result =>{
        if (result.code == 0){
            ElMessage.success(result.msg)
            dialogFormVisible.value = false
            getAdminInfo()
        }else {
            ElMessage.error(result.msg)
        }
    })
}



</script>

<template>
    <!-- element-plus中的容器 -->
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <el-menu active-text-color="#ffd04b" background-color="#232323" text-color="#fff" router>
                <!-- 动态生成菜单 -->
                <template v-for="(menu, index) in menuData" :index="index.toString()">
                    <el-sub-menu v-if="menu.children?.length>0 && menu.type!=1" :index="menu.name">
                        <template #title>
                            <component
                                class="icons"
                                :is="menu.icon"
                                style="width: 1em; height: 1em; margin-right: 8px" >
                            </component>
                            <span>{{ menu.name }}</span>
                        </template>
                        <el-menu-item v-for="(child, ind) in menu.children" :index="child.path">
                            <el-icon><component :is="child.icon"></component></el-icon>
                            <span>{{ child.name }}</span>
                        </el-menu-item>
                    </el-sub-menu>
                    <el-menu-item v-else :index="menu.path">
                        <el-icon><component :is="menu.icon"></component></el-icon>
                        <span>{{ menu.name }}</span>
                    </el-menu-item>
                </template>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div><strong>健身房后台管理系统{{ zhansgan }}</strong></div>
                <div class="user-info">
                    <span class="username">{{ adminInfoStore.admin.name || '管理员' }}</span>
                    <el-dropdown placement="bottom-end" @command="handleCommand">
                        <span class="el-dropdown__box">
                            <el-avatar :src="adminInfoStore.admin.avatar?adminInfoStore.admin.avatar:avatar"/>
                            <el-icon>
                                <CaretBottom/>
                            </el-icon>
                        </span>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                                <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
                                <el-dropdown-item command="resetPassword" :icon="EditPen">重置密码</el-dropdown-item>
                                <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>
                </div>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>后台管理 ©2024 Created by Shao</el-footer>
        </el-container>
    </el-container>
    <!--添加编辑弹出框-->
    <el-dialog v-model="dialogFormVisible" :title="title" width="500" :lock-scroll="false">
        <el-form :model="admin">
            <el-form-item label="名字" :label-width="60">
                <el-input v-model="admin.name" autocomplete="off" />
            </el-form-item>
            <el-form-item label="邮箱" :label-width="60">
                <el-input v-model="admin.email" autocomplete="off" />
            </el-form-item>
            <el-form-item label="手机号" :label-width="60">
                <el-input v-model="admin.phone" autocomplete="off" />
            </el-form-item>
            <el-form-item label="照片" :label-width="60">
                <el-upload
                    class="avatar-uploader"
                    action="/api/upload"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :headers="headers"
                >
                    <img v-if="admin.avatar" :src="admin.avatar" class="avatar"/>
                    <el-icon v-else class="avatar-uploader-icon">
                        <Plus/>
                    </el-icon>
                </el-upload>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="updateAdminInfo">
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
    //
    <el-dialog  v-model="dialogResetPasswordDialog" title="重置密码" width="500" :lock-scroll="false">
        <el-form ref="resetForm" :rules="rules" :model="adminPasswordDTO">
            <el-form-item prop="oldPassword" label="原密码" :label-width="100">
                <el-input v-model="adminPasswordDTO.oldPassword" autocomplete="off"/>
            </el-form-item>
            <el-form-item prop="newPassword" label="新密码" :label-width="100">
                <el-input v-model="adminPasswordDTO.newPassword" autocomplete="off"/>
            </el-form-item>
            <el-form-item prop="reNewPassword" label="重复新密码" :label-width="100">
                <el-input v-model="adminPasswordDTO.reNewPassword" autocomplete="off"/>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogResetPasswordDialog = false">取消</el-button>
                <el-button type="primary" @click="resetPassword(resetForm)">
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<style lang="scss" scoped>
    .layout-container {
        height: 100vh;

        .el-aside {
            background-color: #232323;

            &__logo {
                height: 120px;
                background: url('@/assets/logo.png') no-repeat center / 120px auto;
            }

            .el-menu {
                border-right: none;
            }
        }

        .el-header {
            background-color: #fff;
            display: flex;
            align-items: center;
            justify-content: space-between;

            .el-dropdown__box {
                display: flex;
                align-items: center;

                .el-icon {
                    color: #999;
                    margin-left: 10px;
                }

                &:active,
                &:focus {
                    outline: none;
                }
            }
        }

        .el-footer {
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 14px;
            color: #666;
        }
    }
    
    // 用户信息区域样式
    .user-info {
        display: flex;
        align-items: center;
        gap: 12px;
        
        .username {
            font-size: 14px;
            color: #606266;
            font-weight: 500;
            white-space: nowrap;
        }
    }
</style>